import axios from 'axios';

const API_URL = 'http://localhost:8000/api/ml';

export interface MLModelParams {
  data: any;
  feature_columns: string[];
  target_column: string;
  test_size: number;
  [key: string]: any; // 其他特定于模型的参数
}

export interface MLModelResult {
  success: boolean;
  message: string;
  metrics?: any;
  model_params?: any;
  feature_importance?: any;
  training_info?: any;
  error?: string;
}

export const mlService = {
  trainLogisticRegression: async (params: MLModelParams): Promise<MLModelResult> => {
    try {
      const response = await axios.post(`${API_URL}/logistic_regression`, params);
      return response.data;
    } catch (error: any) {
      console.error('训练逻辑回归模型出错:', error);
      return {
        success: false,
        message: '训练失败',
        error: error.response?.data?.detail || error.message
      };
    }
  },

  trainRandomForest: async (params: MLModelParams): Promise<MLModelResult> => {
    try {
      const response = await axios.post(`${API_URL}/random_forest`, params);
      return response.data;
    } catch (error: any) {
      console.error('训练随机森林模型出错:', error);
      return {
        success: false,
        message: '训练失败',
        error: error.response?.data?.detail || error.message
      };
    }
  },

  trainSVM: async (params: MLModelParams): Promise<MLModelResult> => {
    try {
      const response = await axios.post(`${API_URL}/svm`, params);
      return response.data;
    } catch (error: any) {
      console.error('训练SVM模型出错:', error);
      return {
        success: false,
        message: '训练失败',
        error: error.response?.data?.detail || error.message
      };
    }
  }
};

export default mlService; 